package com.fzu.mmdbproxyframework.cluster.passcloud;

import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class VMNetworkOperation {
	public ArrayList<HashMap<String, Object>> listVMNetwork(
			HashMap<String, String> params) {
		String result = SendRequest.sendRequest(
				InternetwareCmd.ListVMNetworkCmd, params);
		if (null == result
				|| JSONObject.fromObject(result).getJSONObject(
						InternetwareCmd.ListVMNetworkRes).isEmpty())
			return null;
		ArrayList<HashMap<String, Object>> vmNetworks = new ArrayList<HashMap<String, Object>>();
		JSONArray obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.ListVMNetworkRes).getJSONArray("network");
		for (int i = 0; i < obj.size(); i++) {
			JSONObject o = obj.getJSONObject(i);
			HashMap<String, Object> map = InternetwareTool.JSON2Map(o);
			vmNetworks.add(map);
		}
		return vmNetworks;
	}

	public ArrayList<HashMap<String, Object>> listVlanConfiguration(
			HashMap<String, String> params) {
		String result = SendRequest.sendRequest(
				InternetwareCmd.ListVMNetworkCmd, params);
		if (null == result
				|| JSONObject.fromObject(result).getJSONObject(
						InternetwareCmd.ListVMNetworkRes).isEmpty())
			return null;
		ArrayList<HashMap<String, Object>> vmNetworks = new ArrayList<HashMap<String, Object>>();
		JSONArray obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.ListVMNetworkRes).getJSONArray("network");
		for (int i = 0; i < obj.size(); i++) {
			JSONObject o = obj.getJSONObject(i);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", o.getString("id"));
			map.put("name", o.getString("name"));
			String gateway = o.getString("gateway");
			String netmask = o.getString("netmask");
			HashMap<String, Object> ipInfo = InternetwareTool.getIpRange(
					gateway, netmask);
			map.put("startIp", ipInfo.get("startIp"));
			map.put("endIp", ipInfo.get("endIp"));
			map.put("ips", ipInfo.get("ips"));
			vmNetworks.add(map);
		}
		return vmNetworks;
	}
	public String createNetwork(HashMap<String, String> params){
		String result = SendRequest.sendRequest(
				InternetwareCmd.createNetworkCmd, params);
		if(null == result){
			return "fail";
		}
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.createNetworkRes);
		if(null == obj || obj.has("errorcode")){
			return "fail";
		}
		return obj.getJSONObject("network").getString("id");
	}
	public Boolean updateNetwork(HashMap<String, String> params) {
		Boolean success = false;
		String result = SendRequest.sendRequest(
				InternetwareCmd.updateNetworkCmd, params);
		if(null == result){
			return success;
		}
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.updateNetworkRes);
		if(null == obj || obj.has("errorcode")){
			return success;
		}
		JSONObject obj2 = QueryAsyncJob.sendAsynRequest(obj.getString("jobid"));
		if (obj2.getInt("jobstatus") == 1) {
			success = true;
			return success;
		}
		return success;
	}
	
	public Boolean restartNetwork(HashMap<String, String> params) {
		Boolean success = false;
		String result = SendRequest.sendRequest(
				InternetwareCmd.restartNetworkCmd, params);
		if(null == result){
			return success;
		}
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.restartNetworkRes);
		if(null == obj || obj.has("errorcode")){
			return success;
		}
		JSONObject obj2 = QueryAsyncJob.sendAsynRequest(obj.getString("jobid"));
		if (obj2.getInt("jobstatus") == 1) {
			success = true;
			return success;
		}
		return success;
	}
	public Boolean deleteNetwork(HashMap<String, String> params) {
		Boolean success = false;
		String result = SendRequest.sendRequest(
				InternetwareCmd.deleteNetworkCmd, params);
		if(null == result){
			return success;
		}
		JSONObject obj = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.deleteNetworkRes);
		if(null == obj || obj.has("errorcode")){
			return success;
		}
		JSONObject obj2 = QueryAsyncJob.sendAsynRequest(obj.getString("jobid"));
		if (obj2.getInt("jobstatus") == 1) {
			success = true;
			return success;
		}
		return success;
	}
	public static void main(String[] args) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("projectid", "3dd73431-0c65-4dba-838f-ec6a37419fe2");
		System.out.println(new VMNetworkOperation().listVMNetwork(null));
//		params.put("id", "b29661aa-8bd9-4770-8363-e11a7bed05d9");
//		System.out.println(new VMNetworkOperation().deleteNetwork(params));
//		params.put("zoneid", "b22d554a-549b-40c8-a72c-b8eccf1632f5");
//		params.put("name", "福州大学超级计算机中心私有网络");
////		params.put("acltype", "account");
//		params.put("displaytext", "福州大学超级计算机中心共享网络");
//		params.put("vlan", "42");//StartIp/endIp/gateway/netmask31be0870-14b6-4afe-a422-6563598e58f5
////		params.put("startip", "192.168.5.51");
////		params.put("endip", "192.168.5.100");
//		params.put("gateway", "192.168.7.1");
//		params.put("netmask", "255.255.255.0");
//		params.put("networkofferingid", "d9ab9443-c3fe-4538-a505-7b057fd8b892");
//		params.put("projectid", "3dd73431-0c65-4dba-838f-ec6a37419fe2");
//		System.out.println(new VMNetworkOperation().createNetwork(params));
//		params.put("id", "57b558cf-bbac-4f00-9a4b-fb313c60ce6a");
//		System.out.println(new VMNetworkOperation().restartNetwork(params));
	}
}
